package com.mowh.multithreading.two.phase.termination.latch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author mowh1gd
 * @create 2023/11/27 15:24
 */
public class Main {
    private static final int TASKS = 10;

    public static void main(String[] args) {
        System.out.println("BEGIN");
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        CountDownLatch doneLatch = new CountDownLatch(TASKS);

        try {
            for (int t = 0; t < TASKS; t++) {
                executorService.execute(new MyTask(doneLatch, t));//10个任务，5个线程轮流执行
            }
            System.out.println("AWAIT");
            doneLatch.await();//等待指定次数的某操作发生之后（即CountDownLatch的await方法在countdown计数为0时中断），这是多个线程同步的方法之一。
        } catch (InterruptedException e) {
        } finally {
            executorService.shutdown();
            System.out.println("END");
        }
    }
}
